<template>
    <h1>用户投资赎回页面</h1>

    <table>
        <tr>
            <td>项目名称</td>
            <td>{{ info.I_Name }}</td>
        </tr>
        <tr>
            <td>项目投资总金额</td>
            <td>{{ info.I_Budget }}</td>
        </tr>
        <tr>
            <td>用户已投金额</td>
            <td>{{ info.UI_Money }}</td>
        </tr>
        <tr>
            <td>赎回金额</td>
            <td>
                 <input type="text" v-model="Money">
            </td>
        </tr>
        <tr>
            <td>
                <input type="button" value="确认赎回" @click="AddData">
            </td>
            <td></td>
        </tr>
    </table>


</template>

<script setup lang="ts">
import {useRoute} from 'vue-router'
import {ref,onMounted} from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'

const router = useRouter()

const route = useRoute()

console.log(route.params.UI_Id)

const info = ref({
    UI_Money:'',
    I_Name:'',
    I_Budget:''
})

const Money = ref('')

onMounted(()=>{
    axios({
        method:'get',
        url:'http://localhost:53279/api/Invest/GetUserInvestInfo',
        params:{
            UIid:route.params.UI_Id
        }
    })
    .then(res=>{
        console.log(res.data)
        info.value = res.data[0]
    })
    .catch(res=>{
        console.log(res)
    })
})

const AddData = ()=>{

    if(Number(Money.value)>Number(info.value.UI_Money)){
        alert('用户赎回金额不能小于用户投资金额');
        return;
    }

    axios({
        method:'post',
        url:'http://localhost:53279/api/Invest/AddUserInvestRedeem',
        data:{
            UIR_Money:Money.value,
            UI_Id:route.params.UI_Id
        }
    })
    .then(res=>{
        console.log(res)
        if(res.data>0){
            alert('用户赎回成功')
            router.push('/ShowAllInversRedeer');
        }
        else{
            alert('用户赎回失败')
        }
    })
    .catch(res=>{
        console.log(res)
    })
}

</script>